/*
 * Copyright 2009 Yannick Stucki (yannickstucki.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yannickstucki.android.musicqueue;

import android.database.Cursor;

/**
 * Represents a single song and stores all information associated with it.
 * Fields are public because getters and setters are expensive on a mobile
 * device.
 * 
 * @author Yannick Stucki (yannickstucki@gmail.com)
 * 
 */
public class Song implements Comparable<Song> {

  /**
   * The index of the display string for portrait.
   */
  public static final int DISPLAY_INDEX_PORTRAIT = 0;

  /**
   * The index of the display string for landscape.
   */
  public static final int DISPLAY_INDEX_LANDSCAPE = 1;

  /**
   * The number of different display sings. Currently there's one for portrait
   * and one for landscape.
   */
  public static final int NUMBER_DISPLAY_STRINGS = 2;

  /**
   * The position of the title in a cursor.
   */
  private static final int CURSOR_POSITION_TITLE = 0;

  /**
   * The position of the album in a cursor.
   */
  private static final int CURSOR_POSITION_ALBUM = 1;

  /**
   * The position of the artist in a cursor.
   */
  private static final int CURSOR_POSITION_ARTIST = 2;

  /**
   * The position of the path in a cursor.
   */
  private static final int CURSOR_POSITION_PATH = 3;

  /**
   * The position of the duration in a cursor.
   */
  private static final int CURSOR_POSITION_DURATION = 4;

  /**
   * The title of the song.
   */
  public String title;

  /**
   * The album the song belongs to.
   */
  public String album;

  /**
   * The artist that created the song.
   */
  public String artist;

  /**
   * The path identifies the location of the song for playing.
   */
  public String path;

  /**
   * The duration of the song in milliseconds.
   */
  public int duration;

  /**
   * Every song is assigned an index as a fast and unique identifier. This is
   * error prone because indexes might change when new songs are added. Indexes
   * are currently used to store the state of the app.
   * 
   * TODO: delete this field and use uniqueId instead.
   */
  public int index;

  /**
   * Whether the song belongs to the favorites or not.
   */
  public boolean starred = false;

  /**
   * An array of all possible displayed Strings.
   */
  public String[] displayStrings = new String[NUMBER_DISPLAY_STRINGS];

  /**
   * The index of the current display string.
   */
  public static int currentDisplayStringIndex = DISPLAY_INDEX_PORTRAIT;

  /**
   * A unique identifier for every song. It's a concatenation of title, album,
   * artist path and duration.
   * 
   * TODO: shouldn't path be unique?
   */
  public String uniqueId;

  /**
   * Creates a string by reading out the values of a cursor. The cursor must be
   * set to a valid location.
   */
  public Song(Cursor cursor) {
    init(cursor.getString(CURSOR_POSITION_TITLE), cursor
        .getString(CURSOR_POSITION_ALBUM), cursor
        .getString(CURSOR_POSITION_ARTIST), cursor
        .getString(CURSOR_POSITION_PATH), cursor
        .getInt(CURSOR_POSITION_DURATION));
  }

  /**
   * Constructor used by the database since only the unique Id is needed and
   * stored in the database.
   */
  public Song(String uniqueId) {
    this.uniqueId = uniqueId;
  }

  /**
   * Initializes the song with all the required attributes.
   */
  private void init(String titleIn, String albumIn, String artistIn,
      String pathIn, int durationIn) {
    this.title = titleIn;
    this.album = albumIn;
    this.artist = artistIn;
    this.path = pathIn;
    this.duration = durationIn;

    uniqueId = artist + title + album + path + duration;
    // Input sanitation... Those are not needed and can crash the database.
    // TODO: maybe use a sanitizer class
    uniqueId = uniqueId.replaceAll("'", "");
    uniqueId = uniqueId.replaceAll("\"", "");

    displayStrings[DISPLAY_INDEX_PORTRAIT] = title;
    displayStrings[DISPLAY_INDEX_LANDSCAPE] = artist + " - " + title;
  }

  /**
   * Songs are compared by comparing their unique ID.
   */
  @Override
  public int compareTo(Song another) {
    return this.uniqueId.compareTo(another.uniqueId);
  }

  /**
   * The hash code is generated out of the unique ID.
   */
  @Override
  public int hashCode() {
    return uniqueId.hashCode();
  }

  /**
   * The unique ID is used for quality testing.
   */
  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    Song other = (Song) obj;
    if (uniqueId == null) {
      if (other.uniqueId != null) {
        return false;
      }
    } else if (!uniqueId.equals(other.uniqueId)) {
      return false;
    }
    return true;
  }

  /**
   * Used for filtering songs.
   */
  @Override
  public String toString() {
    return displayStrings[Song.DISPLAY_INDEX_LANDSCAPE];
  }
}
